//
//  RadioLookView.m
//  SunXi
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "RadioLookView.h"

@implementation RadioLookView
//http://pianke.me/webview/565479845d774312708b45bd?style=ting3
{   // 进度轮
    UIActivityIndicatorView *acView;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.backgroundColor = [UIColor whiteColor];
    self.webView = [[UIWebView alloc] init];
    [self addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 64, 0));
    }];
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    self.webView.backgroundColor = [UIColor whiteColor];
    // 指定进度轮的大小
    acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    // 设置样式
    acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    // 设置进度轮的中心
    acView.center = self.center;
    [self addSubview:acView];
    
    [self createData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createData) name:@"更换音乐" object:nil];
    
   }
- (void)createData{
    NSString *url = @"http://api2.pianke.me/ting/info";
    NSString *tingid = [[[RadioSavemodel shareSingleton].playlistArr objectAtIndex:[RadioSavemodel shareSingleton].playNumber] radioid];
    NSDictionary *para = @{@"tingid":tingid,@"client":@"2"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:url WithParameters:para Block:^(id result) {
        
        
        [self loadWebView:result];

    }];
}
- (void)loadWebView:(id)result{
    NSString *url = [NSString stringWithFormat:@"http://pianke.me/webview/%@?style=ting3",result[@"data"][@"contentid"]];
    NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSString *datastr = [aString stringByReplacingOccurrencesOfString:@"#f7f7f7" withString:@"#FFFFFF"];
        [self.webView loadHTMLString:datastr baseURL:nil];
        
    }];
    [dataTask resume];

    
    
    
    
}




- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    // 进度轮开始
    [acView startAnimating];
    
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 进度轮结束
    [acView stopAnimating];
    //  加载完成时,去掉loading效果
    [acView removeFromSuperview];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
